package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IReleaseMatchView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class ReleaseMatchPresenter
{
    private BaseBean baseBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IReleaseMatchView releaseMatchView;
    private Context context;
    private BaseView baseView;
    public ReleaseMatchPresenter(Context context, IReleaseMatchView releaseMatchView, BaseView baseView)
    {
        this.context = context;
        this.releaseMatchView = releaseMatchView;
        this.baseView=baseView;
    }

    /**
     * 发布约战
     */
    public void releaseMatch()
    {
        if("".equals(releaseMatchView.type_id()))
        {
            Toast.makeText(context,"请选择运动项目",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.city_id()))
        {
            Toast.makeText(context,"请选择城市",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.num()))
        {
            Toast.makeText(context,"请选择约战人数",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.match_type()))
        {
            Toast.makeText(context,"请选择付款方式",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.content()))
        {
            Toast.makeText(context,"请输入活动说明",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.stime()))
        {
            Toast.makeText(context,"请选择开始时间",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.etime()))
        {
            Toast.makeText(context,"请选择结束时间",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.addr()))
        {
            Toast.makeText(context,"请输入活动地点",Toast.LENGTH_SHORT).show();
        }else if("".equals(releaseMatchView.city_id())){
            Toast.makeText(context,"请选择地区",Toast.LENGTH_SHORT).show();
        }
        else
        {
            String sTime=getsTime();
            String eTime=geteTime();
            baseView.showLoading();
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.publishMatch(UrlData.BASEURL + UrlData.PUBLISHMATCH, releaseMatchView.type_id(),
                    releaseMatchView.city_id(), releaseMatchView.num(),
                    releaseMatchView.match_type(), releaseMatchView.content(), sTime,
                    eTime, releaseMatchView.addr(),
                    SharePrefencesUtil.getUser_id(context), releaseMatchView.price(), handler);
        }

    }

    private String  getsTime()
    {
        String startTime=releaseMatchView.stime();
        String [] year=startTime.split("年");
        String [] month=year[1].split("月");
        String [] day=month[1].split("日");
        String sTime=year[0]+"-"+month[0]+"-"+day[0]+day[1];
        return sTime;
    }
    private String  geteTime()
    {
        String startTime=releaseMatchView.etime();
        String [] year=startTime.split("年");
        String [] month=year[1].split("月");
        String [] day=month[1].split("日");
        String eTime=year[0]+"-"+month[0]+"-"+day[0]+day[1];
        return eTime;
    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            baseView.hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:

                        baseBean= gson.fromJson(result, BaseBean.class);
                        if(0==baseBean.getState())
                        {
                            Toast.makeText(context, "发布约战成功", Toast.LENGTH_SHORT).show();
                        }else
                        {
                            baseView.showError(baseBean.getMsg());
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.showError(result);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}